package org.jeecg.modules.lab.items.temp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.entity.LabItems;
import org.jeecg.modules.lab.items.item.service.ILabItemsService;
import org.jeecg.modules.lab.items.temp.entity.*;
import org.jeecg.modules.lab.items.temp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Api(tags="实验项目模板_模板复制")
@RestController
@RequestMapping("/items/labItemsTemp")
@Slf4j
public class LabItemsTempController extends JeecgController<LabItemsTempDevice, ILabItemsTempDeviceService> {

    @Autowired
    private ILabItemsTempSampleService labItemsTempSampleService;

    @Autowired
    private ILabItemsTempNormService labItemsTempNormService;

    @Autowired
    private ILabItemsTempPersonService labItemsTempPersonService;

    @Autowired
    private ILabItemsTempTargetService labItemsTempTargetService;

    @Autowired
    private ILabCronJobService labCronJobService;

    @Autowired
    private ILabItemsService labItemsService;

    /**
     *   模板复制
     *
     * @return
     */
    @ApiOperation(value="实验项目模板_模板复制", notes="实验项目模板_模板复制")
    @PostMapping(value = "/copyModal")
    public Result<String> copyModal(@RequestParam(name="oldId",required=true) String oldId,
                                    @RequestParam(name="newId",required=true) String newId) {
        //样品信息
        QueryWrapper<LabItemsTempSample> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("item_id", oldId);
        List<LabItemsTempSample> itemsTempSampleList = labItemsTempSampleService.list(queryWrapper1);
        for(LabItemsTempSample sample:itemsTempSampleList){
            sample.setId(null);
            sample.setCreateTime(null);
            sample.setUpdateBy(null);
            sample.setUpdateTime(null);
            sample.setItemId(newId);
            labItemsTempSampleService.save(sample);
        }
        //检测条件信息
        QueryWrapper<LabItemsTempNorm> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("item_id", oldId);
        List<LabItemsTempNorm> itemsTempNormList = labItemsTempNormService.list(queryWrapper2);
        for(LabItemsTempNorm norm:itemsTempNormList){
            norm.setId(null);
            norm.setCreateTime(null);
            norm.setUpdateBy(null);
            norm.setUpdateTime(null);
            norm.setItemId(newId);
            labItemsTempNormService.save(norm);
        }

        //检测信息
        QueryWrapper<LabItemsTempTarget> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("item_id", oldId);
        List<LabItemsTempTarget> itemsTempTargetList = labItemsTempTargetService.list(queryWrapper3);
        for(LabItemsTempTarget target:itemsTempTargetList){
            target.setId(null);
            target.setCreateTime(null);
            target.setUpdateBy(null);
            target.setUpdateTime(null);
            target.setItemId(newId);
            labItemsTempTargetService.save(target);
        }

        //人员信息
        QueryWrapper<LabItemsTempPerson> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("item_id", oldId);
        List<LabItemsTempPerson> itemsTempPersonList = labItemsTempPersonService.list(queryWrapper4);
        for(LabItemsTempPerson person:itemsTempPersonList){
            person.setId(null);
            person.setCreateTime(null);
            person.setUpdateBy(null);
            person.setUpdateTime(null);
            person.setItemId(newId);
            labItemsTempPersonService.save(person);
        }

        if (MyEnum.sync){
            long time = new Date().getTime();
            List<LabCronJob> jobs = new ArrayList<>();
            jobs.add(new LabCronJob("lab_items_temp_sample","0",time,1));
            jobs.add(new LabCronJob("lab_items_temp_norm","0",time,2));
            jobs.add(new LabCronJob("lab_items_temp_target","0",time,3));
            jobs.add(new LabCronJob("lab_items_temp_person","0",time,4));
            labCronJobService.saveBatch(jobs);
        }
        return Result.OK("模板复制成功！");
    }


    /**
     *   模板复制
     *
     * @return
     */
    @ApiOperation(value="实验项目模板_模板复制s", notes="实验项目模板_模板复制s")
    @PostMapping(value = "/copyModals")
    public Result<String> copyModals(@RequestParam(name="oldId",required=true) String oldId,
                                    @RequestParam(name="newName",required=true) String newName) {
        //查找没有模板的项目
        List<LabItems> labItemsList = labItemsService.getItemsListByName(newName);
        //样品信息
        QueryWrapper<LabItemsTempSample> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("item_id", oldId);
        List<LabItemsTempSample> itemsTempSampleList = labItemsTempSampleService.list(queryWrapper1);
        //检测条件信息
        QueryWrapper<LabItemsTempNorm> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("item_id", oldId);
        List<LabItemsTempNorm> itemsTempNormList = labItemsTempNormService.list(queryWrapper2);
        //检测信息
        QueryWrapper<LabItemsTempTarget> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("item_id", oldId);
        List<LabItemsTempTarget> itemsTempTargetList = labItemsTempTargetService.list(queryWrapper3);
        //人员信息
        QueryWrapper<LabItemsTempPerson> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("item_id", oldId);
        List<LabItemsTempPerson> itemsTempPersonList = labItemsTempPersonService.list(queryWrapper4);
        //复制模板
        for (LabItems item: labItemsList) {
            String newId = item.getId();
            //样品信息
            for(LabItemsTempSample sample:itemsTempSampleList){
                sample.setId(null);
                sample.setCreateTime(null);
                sample.setUpdateBy(null);
                sample.setUpdateTime(null);
                sample.setItemId(newId);
                labItemsTempSampleService.save(sample);
            }
            //检测条件信息
            for(LabItemsTempNorm norm:itemsTempNormList){
                norm.setId(null);
                norm.setCreateTime(null);
                norm.setUpdateBy(null);
                norm.setUpdateTime(null);
                norm.setItemId(newId);
                labItemsTempNormService.save(norm);
            }
            //检测信息
            for(LabItemsTempTarget target:itemsTempTargetList){
                target.setId(null);
                target.setCreateTime(null);
                target.setUpdateBy(null);
                target.setUpdateTime(null);
                target.setItemId(newId);
                labItemsTempTargetService.save(target);
            }
            //人员信息
            for(LabItemsTempPerson person:itemsTempPersonList){
                person.setId(null);
                person.setCreateTime(null);
                person.setUpdateBy(null);
                person.setUpdateTime(null);
                person.setItemId(newId);
                labItemsTempPersonService.save(person);
            }

        }
        return Result.OK("模板复制成功！");
    }
}
